package com.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.action.UserAction;
import com.bean.UserBean;
import com.exception.DBException;
import com.exception.UserException;
import com.util.HandleInfo;
import com.util.Regex;
import com.util.StateInfo;

public class UserAddServlet extends HttpServlet
{

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		// get user infomation from form
		String userid = request.getParameter("userid");
		String firstname = request.getParameter("firstname");
		String lastname = request.getParameter("lastname");
		String age = request.getParameter("age");
		String gender = request.getParameter("gender");
		String email = request.getParameter("email");
		String phone = request.getParameter("phone");
		String remark = request.getParameter("remark");

		// validate the information
		if (null == userid || null == firstname || null == lastname
				|| null == age || null == email || null == phone
				|| null == remark)
		{
			HandleInfo.handle(request, response, StateInfo.INNER_CONFIRM_FAIL);
			return;
		}
		if ("".equals(userid) || "".equals(firstname) || "".equals(lastname)
				|| "".equals(email))
		{
			HandleInfo.handle(request, response, StateInfo.INNER_CONFIRM_FAIL);
			return;
		}
		if (userid.length() != 8 || firstname.length() > 30
				|| lastname.length() > 30 || email.length() > 50
				|| phone.length() > 13)
		{
			HandleInfo.handle(request, response, StateInfo.INNER_CONFIRM_FAIL);
			return;
		}
		if (null != gender)
		{
			if (!"male".equals(gender) && !"female".equals(gender))
			{
				HandleInfo.handle(request, response,
						StateInfo.INNER_CONFIRM_FAIL);
				System.out.println(gender);
				return;
			}
		}
		if (null == gender)
		{
			gender = "";
		}
		if (!Regex.checkUserID(userid) || !Regex.checkName(firstname)
				|| !Regex.checkName(lastname) || !Regex.checkEmail(email))
		{
			HandleInfo.handle(request, response, StateInfo.INNER_CONFIRM_FAIL);
			return;
		}
		if (!"".equals(phone))
		{
			if (!Regex.checkPhone(phone))
			{
				HandleInfo.handle(request, response,
						StateInfo.INNER_CONFIRM_FAIL);
				return;
			}
		}
		if (!"".equals(age))
		{
			if (!Regex.checkAge(age))
			{
				HandleInfo.handle(request, response,
						StateInfo.INNER_CONFIRM_FAIL);
				return;
			}
		}

		// generate a bean by form data
		UserBean userBean = new UserBean();
		userBean.setUserID(userid);
		userBean.setFirstname(firstname);
		userBean.setLastname(lastname);
		userBean.setAge(age);
		userBean.setGender(gender);
		userBean.setEmail(email);
		userBean.setRemark(remark);
		userBean.setPhone(phone);

		// add to database
		try
		{
			new UserAction().addUser(request, response, userBean);
		}
		catch (DBException e)
		{
			// fail to get a connection (database)
			HandleInfo.handle(request, response, e.getMessage());
			return;
		}
		catch (UserException e)
		{
			// fail to execute the transaction
			HandleInfo.handle(request, response, e.getMessage());
			return;
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		doGet(request, response);
	}

}
